package team.fjut.cf.controller;


import org.springframework.web.bind.annotation.*;
import team.fjut.cf.config.interceptor.annotation.LoginRequired;
import team.fjut.cf.pojo.enums.ResultCode;
import team.fjut.cf.pojo.po.ContestRegisterUserPO;
import team.fjut.cf.pojo.vo.ResultJson;
import team.fjut.cf.pojo.vo.UserSignUpVO;
import team.fjut.cf.service.ContestRegisterService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhuoren [2021/3/23]
 */
@RestController
@CrossOrigin
@RequestMapping("/CSingup")
public class ContestSignupController {
    @Resource
    ContestRegisterService contestRegisterService;


    @LoginRequired
    @GetMapping("/getSingupStatus")
    public ResultJson getContestInfo(@RequestParam("username") String username,@RequestParam("cid") Integer contestId) {
        ResultJson resultJson = new ResultJson();
        ContestRegisterUserPO contestRegisterUserPO= contestRegisterService.selectByUsername(username,contestId);
        resultJson.addInfo(contestRegisterUserPO);
        return resultJson;
    }

    //@LoginRequired
    @GetMapping("/getSignUpList")
    public ResultJson getSignUpList(@RequestParam("pageNum") Integer pageNum,
                                    @RequestParam("pageSize") Integer pageSize,
                                    @RequestParam("ContestId") Integer ContestId){
        ResultJson resultJson = new ResultJson();
        if (null == pageNum) {
            pageNum = 1;
        }
        if (null == pageSize || pageSize > 100) {
            pageSize = 50;
        }

        List<UserSignUpVO> userSignUpVOS = contestRegisterService.getSignUpList(pageNum,pageSize,ContestId);
        int count = contestRegisterService.getCount(ContestId);
        resultJson.addInfo(userSignUpVOS);
        resultJson.addInfo(count);
        return resultJson;
    }

   // @LoginRequired
    @GetMapping("/insertUserSign")
    public ResultJson insertUserSign(@RequestParam("username") String username,@RequestParam("ContestId") Integer ContestId){
        ResultJson resultJson = new ResultJson();

        int flag = contestRegisterService.insertUserSign(username,ContestId);
        if(flag == 1)
            resultJson.setStatus(ResultCode.REQUIRED_SUCCESS);

        return resultJson;
    }

}
